package com.sxc.OnlineStore.user.servlet;

import com.sxc.OnlineStore.user.dao.UserIm;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //得到前端传过来的值
        String op = request.getParameter("op");
        String user_Name = request.getParameter("userName");
        String user_PW = request.getParameter("userPW");

        PrintWriter out = response.getWriter();//定义输出到jsp的对象
        /*UserConfirmService userConfirmService = new UserConfirmService();
        userConfirmService.addUser(user_Name,user_PW);*/

        if(op.equals("addUser")){
            UserIm addUser = new UserIm();

            if (addUser.QueryUser(user_Name)){
                addUser.AddUser(user_Name,user_PW);
                //request.setAttribute("userName",user_Name);
<<<<<<< HEAD

=======
                int userPK = addUser.GetUserPK(user_Name);
>>>>>>> 还没有实现在servlet中获取sessiond
                //使用request对象的getSession()获取session，如果session不存在则创建一个
                HttpSession session = request.getSession();

                session.setAttribute("userName",user_Name);
<<<<<<< HEAD
=======
                session.setAttribute("userPK",userPK);
>>>>>>> 还没有实现在servlet中获取sessiond

                request.getRequestDispatcher("home.jsp").forward(request,response);
            }else {
                out.write("<script>alert(\"账户已经存在了哈~~请重新输入哟\");location.replace(\"user_register.jsp\");</script></script>");

            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
